<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FeedbackAppeal;
use App\Admin\Renderable\FarmerImgs;
use App\Models\IntegralRule;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class FeedbackAppealController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FeedbackAppeal(), function (Grid $grid) {
            $grid->model()->orderBy('type', 'asc');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function($user_id){
                $user = User::find($user_id);
                return $user ? ($user -> real_name ?: $user -> user_name) : '';
            });
            $grid->column('type')->display(function ($ems_type) {
                // 诉求类型;投诉反馈1、BUG反馈2、功能建议3
                switch ($ems_type) {
                    case 1:
                        return '投诉反馈';
                        break;
                    case 2:
                        return 'BUG反馈';
                        break;
                    default:
                        return '功能建议';
                }
            });

            $grid->column('content');
            $grid->column('contact');
//            $grid->column('imgs');
            $grid->column('imgs', '图片')
                ->display('查看')
                ->expand(function () {
                    return FarmerImgs::make(['imgs' => $this->imgs]);
                });

            $grid->column('status')->help('已处理，奖励积分 10分')->select([0 => '待处理', 1 => '处理中', 2 => '已处理'])->sortable();
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->disableEditButton(); // 禁用编辑按钮
            $grid->disableViewButton(); // 禁用详情按钮
            $grid->disableCreateButton(); // 禁用新增按钮
            $grid->quickSearch(['content'])->placeholder('搜索...');
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('type', '类型筛选', [1 => '投诉反馈', 2 => 'BUG反馈', 3 => '功能建议']);
                $selector->selectOne('status', '状态筛选', [0 => '待处理', 1 => '处理中', 2 => '已处理']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FeedbackAppeal(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('content');
            $show->field('contact');
            $show->field('imgs');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FeedbackAppeal(), function (Form $form) {
            $form->display('id');
            $form->text('type');
            $form->text('content');
            $form->text('contact');
            $form->text('imgs');
            $form->text('status');
        
            $form->display('created_at');
            $form->display('updated_at');
            $form->submitted(function(Form $form){

                $id = $form -> getKey();
                $feedbackAppeal = $form -> model() -> find($id);

                if($form -> status == 2 && $feedbackAppeal->status != 2){

                    $integralRule = IntegralRule::rule(9,$feedbackAppeal -> user_id,$id);
                }
            });
        });
    }
}
